module.exports = (EdtApi) => {
    "use strict";
    let badges, chatId = "", count = 0, title = document.title, chatTitle = "", handler = null;
    function isGroup() {
        return chatTitle?.includes("(") && chatTitle?.includes(")");
    }
    return {
        config: {
            friendList: 'test',
            chat: '#wrap',
            input: 'div.chatWriteArea div.mentions-input textarea',
            msgBox: 'div.chatRoom div.cChatList div[data-viewname="DChattingRoomMessageListView"]',
            inMsg: 'div._childViewContainer>div.logWrap.logFriend div.msg div.messageBodyWrap span.txt:not(.trans-inited)',
            outMsg: 'div._childViewContainer>div.logWrap.logMy div.msg div.messageBodyWrap span.txt:not(.trans-inited)',
            sendBtn: 'div.chatButtonArea div.chatSubmit button._sendMessageButton',
            setting: "div.chatButtonArea ul.chatUploadList>li.chatUploadListItem",
            tips: {
                in: 'header>div.sChatHeaderInfoBox',
                out: 'section.sChatBottom div.cChatWriteWrap'
            },
            // crm: {
            //     top: '#detail-header>div.css-175oi2r',
            //     left: ""
            // },
            placeholder: {
                selector: "div.chatWriteArea div.mentions-input textarea",
                attr: "placeholder"
            },
            autoCount: 20
        },
        init(_handler) {
            handler = _handler;
            document.addEventListener('DOMContentLoaded', () => {
                let url = location.href;
                if (url.includes("/chat/")) {
                    chatId = url.split("/chat/")[1];
                    this.updateTitle();
                }
            });
            document.addEventListener('DOMSubtreeModified', () => {
                if (title !== document.title) {
                    title = document.title;
                    if (title && "null" != title && !title.startsWith("聊天室")) {
                        chatTitle = document.title?.split("|")[0];
                        this.updateTitle();
                    }
                }
            });
        },
        updateTitle() {
            if (chatId && chatTitle) {
                EdtApi.sendToHost('update-band-title', {
                    chatId,
                    chatTitle
                });
                handler?.initChat(null);
            }
        },
        getUnread() {
            badges = document.querySelector('header ul.widgetList>li.widgetItem>button[aria-controls="gnbChatRegion"]>mark');
            const visibility = window.getComputedStyle(badges).getPropertyValue('display');
            if (visibility !== 'none') {
                count = badges ? EdtApi.safeParseInt(badges.textContent || "0") : 0;
            } else {
                count = 0;
            }
            return count;
        },
        getChatInfo() {
            return new Promise((resolve) => {
                console.log("getChatInfo:", chatId);
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getChatTitle() {
            return chatTitle;
        },
        getText(msgBox) {
            let text = '';
            try {
                if (msgBox.querySelector('img')) {
                    let childNodes = msgBox.childNodes;
                    childNodes?.forEach(child => {
                        if (child.nodeName == "IMG") {
                            text += child.getAttribute("alt") || "";
                        } else {
                            text += child.textContent;
                        }
                    });
                } else {
                    text = msgBox.textContent;
                }
            } catch (error) {
                console.error(error);
            }
            return text;
        },
        getIptMsg(inputEl) {
            return inputEl?.value;
        },
        getUserInfo() {
            try {
                let avatarEl = document.querySelector('header ul.widgetList>li.widgetItem.-positionR span.uProfile img');
                let avatar = avatarEl?.getAttribute("src") || "";
                return {
                    avatar,
                    userName: "",
                    account: "",
                    toDataUrl: true
                };
            } catch (error) {
                return null;
            }
        }
    }
};